package com.ruoyi.web.controller.orders;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.orders.pojo.ZxGoods;
import com.ruoyi.orders.service.ZxGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author lyt
 * @version 1.0
 * @description: TODO
 * @date 2023/12/21 9:40
 */
@Api(value = "商品管理",tags = "商品管理")
@RestController
@RequestMapping("goods")
public class GoodsController extends BaseController{
    @Autowired
    private ZxGoodsService zxGoodsService;

    /**
     * @description: 查询商品列表（分页）
     * @author: Lyt
     * @date: 2023/12/21 10:00
     * @param keyword
     * @return:
    */

    @ApiOperation("根据商品名模糊查询商品列表")
    @GetMapping("list")
    public TableDataInfo getGoodsList(String keyword){
        startPage();  // 此方法配合前端完成自动分页
        List<ZxGoods> list = zxGoodsService.getGoodsList(keyword);
        return getDataTable(list);
    }
    /**
     * @description: 新增商品
     * @author: Lyt
     * @date: 2023/12/21 10:57
     * @param zxGoods
     * @return:
    */
    @ApiOperation("新增商品")
    @PostMapping("save")
    public AjaxResult saveGoods(@RequestBody ZxGoods zxGoods) {
        try {
            boolean save = zxGoodsService.save(zxGoods);
            if (!save) {
                return AjaxResult.error(200, "fail");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(200, "fail");
        }
    }


    /**
     * @description: 修改商品
     * @author: Lyt
     * @date: 2023/12/21 11:06
     * @param zxGoods
     * @return:
    */
    @ApiOperation("修改商品")
    @PutMapping("update")
    public AjaxResult updateGoodsById(@RequestBody ZxGoods zxGoods){
        try {
            boolean b = zxGoodsService.updateById(zxGoods);
            if(!b){
                return AjaxResult.error();
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    @ApiOperation("通过id删除商品")
    @DeleteMapping("remove/{id}")
    public AjaxResult deleteGoodsById(@PathVariable Integer id){
        try {
            boolean b = zxGoodsService.removeById(id);
            if(!b){
                return AjaxResult.error();
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Integer> ids){
        try {
            boolean b = zxGoodsService.removeBatchByIds(ids);
            if(!b){
                return AjaxResult.error();
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
}
